/*
 * RectangleAttachment.java
 *
 * Created on 19 maart 2008, 9:45
 *
 * @author wijnand.schepens@hogent.be
 */
package model.graphic.objects;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import model.graphic.interfaces.Collapseable;
import model.graphic.locations.Location;
import view.GUI.graphic.GraphicView;

public abstract class RectangleAttachment extends Attachment implements Collapseable {

    protected Location lowerLeft;
    protected Location lowerRight;
    protected Location upperRight;
    protected int width;
    protected int height;
    protected int minSize;
    protected boolean collapsed = false;
    protected String displayName = null;

    public RectangleAttachment() {
    }

    public RectangleAttachment(Graphical base, Location location, int minSize, int width, int height) {
        super(base, location);
        this.width = width;
        this.height = height;
        this.minSize = minSize;
    }

    public void setMinSize(int minSize) {
        this.minSize = minSize;
    }

    public abstract void setLocations(Location location);

    public abstract void adjustLocations(Graphical base, Location location);

    @Override
    public void drag(int relX, int relY) {
        location.setXY(location.getX() + relX, location.getY() + relY);
        setLocations(location);
    }

    @Override
    public boolean contains(int x, int y) {
        return getRectangle().contains(x, y);
    }

    protected Rectangle getHighlightRectangle() {
        return new Rectangle(getUpperLeft().getX() - getX() - 4, getUpperLeft().getY() - getY() - 4, minSize + 4, minSize + 4);
    }

    public Rectangle getRectangle() {
        return new Rectangle(getUpperLeft().getX() - getX(), getUpperLeft().getY() - getY(), getWidth(), getHeight());
    }

    /**
     * @return the collapsed
     */
    public boolean isCollapsed() {
        return collapsed;
    }

    /**
     * @param collapsed the collapsed to set
     */
    public void setCollapsed(boolean collapsed) {
        this.collapsed = collapsed;
    }

    public int getWidth() {
        return lowerRight.getX() - getUpperLeft().getX();
    }

    public int getHeight() {
        return lowerRight.getY() - getUpperLeft().getY();
    }

    public void setWidth(int x) {
        this.width = x;
        setLocations(location);
    }

    public void setHeight(int y) {
        this.height = y;
        setLocations(location);
    }

    /**
     * @return the lowerLeft
     */
    public Location getLowerLeft() {
        return lowerLeft;
    }

    /**
     * @return the lowerRight
     */
    public Location getLowerRight() {
        return lowerRight;
    }

    /**
     * @return the upperRight
     */
    public Location getUpperRight() {
        return upperRight;
    }

    /**
     * @return the location
     */
    public Location getUpperLeft() {
        return location;
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        GraphicView gv = (GraphicView) this.getParent();
        int x = e.getX();
        int y = e.getY();
        int xn = this.getX() + x - dragEndPoint.x;
        int yn = this.getY() + y - dragEndPoint.y;
        this.drag(xn, yn);
        this.getUpperLeft().setXY(xn, yn);
        this.setLocations(this.getUpperLeft());
        gv.repaint();
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    protected void drawCentered(Graphics g, String text, Rectangle bounds) {
        g.setFont(new Font("Arial", Font.PLAIN, 11));
        FontMetrics fm = g.getFontMetrics();
        Rectangle2D textBounds = fm.getStringBounds(text, g);

        int x = bounds.x + 2;
        int y = bounds.y + bounds.height - (int) textBounds.getHeight() / 2 + fm.getDescent();

        g.drawString(text, x, y);
    }
}